import {cbs, pwdReg, phoneReg} from '../../../config/settings.js';
import md5 from 'js-md5';

module.exports = {
	name: 'withdraw',
	data() {
		let emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
		let amountReg = /^(-)?\d+(.\d{2})?$/;
		return {
			isVisible: true,
			withdrawForm: {
				account: '',
				name: '',
				amount: ''
			},
			withdrawRules: {
				account: [{
					validator: (rule, value, cb) => {
						if(!value) {
							cb(new Error('请输入账号'));
						}else if(emailReg.test(value) || phoneReg.test(value)) {
							cb();
						}else {
							cb(new Error('请输入正确的账号'))
						}
					},
					trigger: 'blur'
				}],
				name: [{
					validator: (rule, value, cb) => {
						if(!value) {
							cb(new Error('请输入此项'));
						}else {
							cb();
						}
					},
					trigger: 'blur'
				}],
				amount: [{
					validator: (rule, value, cb) => {
						if(!value) {
							cb(new Error('请输入此项'));
						}else if(!amountReg.test(value)) {
							cb(new Error('提现金额需大于0元'))
						}else {
							cb();
						}
					},
					trigger: 'blur'
				}]
			},
			pwdForm: {
				pwd: ''
			},
			pwdRules: {
				pwd: [{
					validator: (rule, value, cb) => {
						if(!value) {
							cb(new Error('请输入支付密码'));
						}else if(!pwdReg.test(value)) {
							cb(new Error('密码长度需在6-16位之间，同时包含数字和字母'))
						}else {
							cb();
						}
					},
					trigger: 'blur'
				}]
			},
			isFirst: true,
		}
	},
	methods: {
		submit() {
			this.$refs.pwdForm.validate(valid => {
				if(valid) {
					let data = this.$$formData({
						account: this.withdrawForm.account,
						realName: this.withdrawForm.name,
						way: 2,
						amount: this.withdrawForm.amount,
						payPassword: md5(this.pwdForm.pwd)
					})
					this.$$api_manager_withdraw(data, res => {
						if(res.event === 0) {
							this.isFirst = true;
							cbs.submitSuccess.call(this, `提现成功d`);
						}else if(res.event === 4) {
							cbs.submitError.call(this, '余额不足');
						}else {
							cbs.submitError.call(this, `提现失败，${res.msg}`);
						}
					})
				}else {
					return false;
				}
			})
		},
		onNext() {
			this.$refs.withdrawForm.validate(valid => {
				if(valid) {
					this.isFirst = false;
				}else {
					return false;
				}
			})
		},
		onClose() {
			this.isFirst = true;
		}
	},
	computed: {
	}
}